"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : SQL_DDL_Drop_Owned
Case Name   : 用户所拥有的数据库、表空间将不会被移除
Create At   : 2022/08/24
Owner       : @li-xin12345
Description :
    1.创建用户，创建数据库
    2.创建表空间
    3.删除用户所拥有的对象
    4.删除表空间，删除数据库，删除用户
Expect      :
    1.创建成功
    2.创建成功
    3.用户所拥有的数据库、表空间将不会被移除
    4.删除成功
History     :
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class DropOwned(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.user_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.user_name = 'u_drop_owned_0009'
        self.db_name = 'db_drop_owned_0009'
        self.tbsp_name = 'tablespace_drop_owned_0009'
        self.table_name = 't_drop_owned_0009'
        self.view_name = 'v_drop_owned_0009'

    def test_drop_owned(self):
        text = "-----step1:创建用户，创建数据库;expect:创建成功-----"
        self.logger.info(text)
        create_user = f"drop user if exists {self.user_name};" \
            f"create user {self.user_name} " \
            f"sysadmin password '{macro.COMMON_PASSWD}';" \
            f"drop database if exists {self.db_name};" \
            f"create database {self.db_name};" \
            f"alter database {self.db_name} owner to {self.user_name};"
        create_msg = self.primary_sh.execut_db_sql(create_user)
        self.logger.info(create_msg)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      create_msg, '执行失败' + text)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG,
                      create_msg, '执行失败' + text)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      create_msg, '执行失败' + text)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      create_msg, '执行失败' + text)
        self.assertIn(self.constant.ALTER_DATABASE_SUCCESS_MSG,
                      create_msg, '执行失败' + text)

        text = "-----step2:创建表空间;expect:创建成功-----"
        self.logger.info(text)
        sql_cmd = f"drop tablespace if exists {self.tbsp_name};" \
            f"create tablespace {self.tbsp_name} relative " \
            f"location 'tablespace/tablespace_1';" \
            f"alter tablespace {self.tbsp_name} owner to {self.user_name};" \
            f"select t1.spcname, t2.usename, t1.spcacl from " \
            f"pg_tablespace t1 inner join pg_user t2 " \
            f"on t2.usesysid=t1.spcowner where t1.spcname='{self.tbsp_name}';"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLESPCE_CREATE_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertGreater(len(sql_msg.splitlines()), 2, '执行失败' + text)
        self.assertIn(self.tbsp_name, sql_msg.splitlines()[-2], '执行失败' + text)
        self.assertIn(self.user_name, sql_msg.splitlines()[-2], '执行失败' + text)

        text = "-----step3:删除用户所拥有的对象;" \
               "expect:用户所拥有的数据库、表空间将不会被移除-----"
        self.logger.info(text)
        sql_cmd = f"drop owned by {self.user_name} cascade;"
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_name} -W \'{macro.COMMON_PASSWD}\'')
        self.logger.info(sql_msg)
        self.assertIn(self.constant.DROP_OWNED_SUCCESS, sql_msg, '执行失败' + text)
        sql_cmd = f"select t1.spcname, t2.usename, t1.spcacl " \
            f"from pg_tablespace t1 inner join pg_user t2 " \
            f"on t2.usesysid=t1.spcowner where t1.spcname='{self.tbsp_name}';" \
            f"\\l"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertGreater(len(sql_msg.splitlines()), 8, '执行失败' + text)
        self.assertIn(self.tbsp_name, sql_msg.splitlines()[2], '执行失败' + text)
        self.assertIn(self.user_name, sql_msg.splitlines()[2], '执行失败' + text)
        self.assertIn(self.db_name, sql_msg.splitlines()[8], '执行失败' + text)
        self.assertIn(self.user_name, sql_msg.splitlines()[8], '执行失败' + text)

    def tearDown(self):
        text = "-----step4:删除表空间，删除数据库，删除用户，expect:删除成功-----"
        self.logger.info(text)
        drop_cmd = f"drop database {self.db_name};" \
            f"drop tablespace {self.tbsp_name};" \
            f"drop user {self.user_name} cascade;"
        drop_msg = self.primary_sh.execut_db_sql(drop_cmd)
        self.logger.info(drop_msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      drop_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS,
                      drop_msg, '执行失败' + text)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      drop_msg, '执行失败' + text)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
